home *** CD-ROM | disk | FTP | other *** search
- Path: news.halcyon.com!usenet
- From: normanb@halcyon.com (Norm Bryar)
- Newsgroups: comp.lang.c++
- Subject: Re: Mod or if?
- Date: Sat, 10 Feb 1996 16:51:25 GMT
- Organization: Northwest Nexus Inc.
- Message-ID: <4fiidc$crp@news.halcyon.com>
- References: <4fg3jm$fb8@gondor.sdsu.edu>
- NNTP-Posting-Host: blv-pm3-ip26.halcyon.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- weikel@rohan.sdsu.edu (weikel) wrote:
-
- >Which is better?
-
- >x = (x mod 7);
-
- >or
-
- >if (x == 7) x = 0;
-
- >in terms of performance? I know that the mod function is slow
- >,but the if method seems brute-forceish.
-
- You can profile both, I suppose. Generating asm code may not be too
- telling.
-
- But first where is your app getting x? Is x a local int whose value
- is only incremented or decremented by one? Is it a parameter passed
- to your function by possibly dopey users? If other data in the
- procedure is bad, could x ever be incremented by >1 and thus skip
- right over seven?
-
- You may find if( x >=7 ) x = 0; is safer in one case and x=(x mod 7)
- better in another.
-
-